package com.coders.menuactionbar;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MenusandactionbarActivity extends Activity {
    /** Called when the activity is first created. */
	SharedPreferences preferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.Button01);
		// Initialize preferences
		preferences = PreferenceManager.getDefaultSharedPreferences(this);

		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				String username = preferences.getString("username", "n/a");
				String password = preferences.getString("password", "n/a");
				showPrefs(username, password);
			}
		});
		Button buttonChangePreferences = (Button) findViewById(R.id.Button02);
		buttonChangePreferences.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				updatePreferenceValue();
			}
		});
    }
    public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}
    public boolean onOptionsItemSelected(MenuItem item) {
		Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
		return true;
	}
    
    private void showPrefs(String username, String password){
		Toast.makeText(
				MenusandactionbarActivity.this,
				"Input: " + username + " and password: "
						+ password, Toast.LENGTH_LONG).show();

	}
	
	private void updatePreferenceValue(){
		Editor edit = preferences.edit();
		String username = preferences.getString("username", "n/a");
		// We will just revert the current user name and save again
		StringBuffer buffer = new StringBuffer();
		for (int i = username.length() - 1; i >= 0; i--) {
			buffer.append(username.charAt(i));
		}
		edit.putString("username", buffer.toString());
		edit.commit();
		// A toast is a view containing a quick little message for the
		// user. We give a little feedback
		Toast.makeText(MenusandactionbarActivity.this,
				"Reverted string sequence of user name.",
				Toast.LENGTH_LONG).show();
	}

    
    
    
}
    
    
    